package com.csii.adhoc.info.depart.serviceImpl;

import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.info.depart.DepartPo;
import com.csii.adhoc.info.depart.DepartVo;
import com.csii.adhoc.info.depart.mapper.DepartMapper;
import com.csii.adhoc.info.depart.service.DepartService;
import com.csii.adhoc.info.post.service.PostService;
//import com.csii.adhoc.info.user.service.UserService;
import com.csii.adhoc.info.user.service.UserService;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @description: 部门接口实现
 * @author: panChuanJiang
 * @create: 2021-01-26 11:19
 */
@Service("departService")
@Slf4j
public class DepartServiceImpl implements DepartService {
    @Resource
    private DepartMapper departMapper;
    @Autowired
    private UidGenerator uidGenerator;
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public void departMaintain(DepartVo departVo, String optMode) {
        DepartPo departPo = (DepartPo) departVo;
        switch (optMode) {
            case CommonConstant.OPT_MODE_ADD:
                departPo.setCreatedBy(departPo.getUpdatedBy())
                        .setCreatedDt(departPo.getUpdatedDt());
                insertOneDepart(departPo);
                break;
            case CommonConstant.OPT_MODE_DELETE:
                deleteDepartByPrimaryKey(departPo.getDepartId());
                break;
            case CommonConstant.OPT_MODE_UPDATE:
                updateDepartByPrimaryKey(departPo);
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("optMode值不合法:" + optMode);
        }
    }

    @Override
    public List<DepartPo> selectListDepart(DepartPo departPo) {
        return departMapper.selectListDepart(departPo);
    }

    @Transactional
    public void deleteDepartByPrimaryKey(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new ParamValidityException("departId值不合法:" + id);
        }
        departMapper.deleteDepartByPrimaryKey(id);
        postService.deletePostByDepartId(id);
        userService.updateUserClearDepartId(id);
    }

    @Transactional
    public void insertOneDepart(DepartPo departPo) {
        departPo.setDepartId(uidGenerator.getUID() + "")
                .setStatus(Optional.ofNullable(departPo.getStatus()).orElse("1"));//默认为正常为1
        departMapper.insertOneDepart(departPo);
    }

    @Transactional
    public void updateDepartByPrimaryKey(DepartPo departPo) {
        departMapper.updateDepartByPrimaryKeySelective(departPo);
    }
}
